// pages/recharge/recharge.js
const numeral = require("../../vendor/numeral/numeral.min")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        moneyList: [{
                name: '20元',
                value: 20,
            },
            {
                name: '50元',
                value: 50,
            },
            {
                name: '100元',
                value: 100,
            },
            {
                name: '300元',
                value: 300,
            },
            {
                name: '1000元',
                value: 1000,
            },
            {
                name: '5000元',
                value: 5000,
            },
        ],
        currentMoney: null,
        balance: 0,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.loadData()
    },
    loadData() {
        wx.showLoading({
            title: '加载中...',
        })
        getApp().http.post('account/info').then(res => {
            this.setData({
				balance: res.data.recharge_balance ? res.data.recharge_balance : 0
            })
            wx.hideLoading()
        })
    },
    chooseMoney(evt) {
        let currentMoney = evt.currentTarget.dataset.value
        this.setData({
            currentMoney
        })
    },
    bindinput(evt) {
        let value = evt.detail.value
        this.setData({
            currentMoney: value
        })
    },
    moneyFormat() {
        let {
            currentMoney = null
        } = this.data
        this.setData({
            currentMoney: currentMoney ? Math.floor(currentMoney * 100) / 100 : null
        })
    },
    submit() {
        wx.showLoading({
            title: '请稍等',
        })
        getApp().http.post('account/recharge', {
            data: {
                money: this.data.currentMoney
            }
        }).then(res => {
            wx.hideLoading()
            let js = res.data
            wx.requestPayment(Object.assign({
                success: res => {
                    wx.navigateBack({})
                },
                fail: res => {
                    wx.showToast({
                        icon: 'none',
                        title: '充值失败,请重试',
                    })
                }
            }, js))
        })
    }
})